﻿/**********************************天使物流*******************************************
* 功能描述:结算管理-应收费用管理
* 作者:石锦涛
* 时间:2022-01-10
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.SettleAccounts.NewExpensesReceis;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.SettleAccounts.NewExpensesReceis
{
    public class NewReceisAppRepository : DapperDbContextRepository, INewReceisAppRepository
    {
        public NewReceisAppRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 显示应收费用
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<NewExpensesReceiDto>> GetNewRecei(string unit, string account, int? state, string handle, string times, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                PageResponse<NewExpensesReceiDto> doc = new PageResponse<NewExpensesReceiDto>();

                var sql = "select Id as GId,DusinessNumber,TheOwnerUnitl,ClearingForm,Tonnage,UnitPrice,Moneyc,SUBSTRING(CONVERT(Nvarchar(50),BusinessTime),1,10) as BusinessTime,ResponsiblePersonx,Remarku,SUBSTRING(CONVERT(Nvarchar(50), EstablishTime), 1, 10) as EstablishTime,CheckStatet,Collatora,SUBSTRING(CONVERT(Nvarchar(50), CheckTime), 1, 10) as CheckTime,IsDelete from NewExpensesRecei where IsDelete = 0  ";
                if (!string.IsNullOrEmpty(unit))
                {
                    sql += $" and TheOwnerUnitl like '%{unit}%'";
                }
                if (!string.IsNullOrEmpty(account))
                {
                    sql += $" and ClearingForm like '%{account}%'";
                }
                if (state > 0)
                {
                    sql += $" and CheckStatet = '{state}'";
                }
                if (!string.IsNullOrEmpty(handle))
                {
                    sql += $" and ResponsiblePersonx like '%{handle}%'";
                }
                if (!string.IsNullOrEmpty(times))
                {
                    sql += $" and BusinessTime ='{times}'";
                }
                var list = await this.QueryAsync<NewExpensesReceiDto>(sql, null);
                doc.PageTotal = list.Count();
                sql += $" order by Id offset ({pageindex}-1)*{pagesize} rows fetch next {pagesize} rows only";
                var query = await this.QueryAsync<NewExpensesReceiDto>(sql, null);
                doc.PageData = query;
                return doc;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewExpensesReceiDto>> DeleteNewRecei(int Id)
        {
            try
            {
                PageResponse<NewExpensesReceiDto> doc = new PageResponse<NewExpensesReceiDto>();
                var sql = $"update  NewExpensesRecei set IsDelete=1 where Id={Id}";
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 应收费用修改
        /// </summary>
        /// <param name="newExpenses"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> UpdateNewRecei(NewExpensesReceiDto newExpenses)
        {
            try
            {
                var sql = $"update NewExpensesRecei set DusinessNumber='{newExpenses.DusinessNumber}',TheOwnerUnitl='{newExpenses.TheOwnerUnitl}',ClearingForm='{newExpenses.ClearingForm}',Tonnage='{newExpenses.Tonnage}',UnitPrice = '{newExpenses.UnitPrice}',Moneyc = '{newExpenses.Moneyc}',BusinessTime = '{newExpenses.BusinessTime.ToString("yyyy/MM/dd")}',ResponsiblePersonx = '{newExpenses.ResponsiblePersonx}',Remarku = '{newExpenses.Remarku}', EstablishTime = '{newExpenses.EstablishTime.ToString("yyyy/MM/dd")}',CheckStatet = '{newExpenses.CheckStatet}',Collatora = '{newExpenses.Collatora}',CheckTime = '{newExpenses.CheckTime.ToString("yyyy/MM/dd")}',IsDelete = '{newExpenses.IsDelete}' where Id = '{newExpenses.GId}'";
                PageResponse<int> doc = new PageResponse<int>();
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 添加应费用
        /// </summary>
        /// <param name="newExpenses"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> PostNewRecei(NewExpensesReceiDto newExpenses)
        {
            try
            {
                //雪花算法
                var worker = new IdWorker(1, 1);
                var id = worker.NextId().ToString();

                var sql = $"insert NewExpensesRecei values('{id}','{newExpenses.TheOwnerUnitl}','{newExpenses.ClearingForm}','{newExpenses.Tonnage}','{newExpenses.UnitPrice}','{newExpenses.Moneyc}','{newExpenses.BusinessTime.ToString("yyyy/MM/dd")}','{newExpenses.ResponsiblePersonx}','{newExpenses.Remarku}',GETDATE(),'{newExpenses.CheckStatet}','{newExpenses.Collatora}',GETDATE(),'{newExpenses.IsDelete}')";
                PageResponse<int> doc = new PageResponse<int>();
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewExpensesReceiDto>> DeleteNewReceiMser(string Id)
        {
            try
            {
                PageResponse<NewExpensesReceiDto> doc = new PageResponse<NewExpensesReceiDto>();
                var ids = Id.Split(',');
                foreach (var item in ids)
                {
                    var shu = Convert.ToInt32(item);
                    var list = await this.QueryAsync<NewExpensesReceiDto>($"select * from NewExpensesRecei where Id={shu}");
                    if (list != null)
                    {
                        var sql = $"update NewExpensesRecei set IsDelete=1 where Id={shu} ";
                        doc.Code = await this.ExecuteAsync(sql, null);
                    }
                }
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
